package cn.com.sbell.vss.auth.radius;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sourceforge.jradiusclient.RadiusAttribute;
import net.sourceforge.jradiusclient.RadiusAttributeValues;
import net.sourceforge.jradiusclient.RadiusPacket;
import net.sourceforge.jradiusclient.attributes.VendorSpecificAttribute;
import net.sourceforge.jradiusclient.packets.PapAccessRequest;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.ILogCommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VissSession;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.VssException;
import cn.com.sbell.vss.util.RandomGUID;
import cn.com.sbell.vss.util.SessionHolder;
import cn.com.sbell.vss.util.VssValidator;
import cn.com.sbell.vss.csg.vap.util.CrossDomainCredential;
import cn.com.sbell.vss.auth.radius.bean.ServerAddress;

public class InterDomainAuthByRadius implements ICommander, ILogCommander {
	private static final Log log = LogFactory
	.getLog(InterDomainAuthByRadius.class);



private String localIp = "127.0.0.1";

private int nasType = -1;

private VssRadiusClient vssRadiusClient;

	public Response execute(HttpServletRequest request) {
		// TODO Auto-generated method stub
//		 prepare response (include parameters)
		Response response = Response.createResponse(request);

		// get parameters
		String username = Util.getTrimParameter(request, "__username");
		String password = Util.getTrimParameter(request, "__password");
		
		
		if (Util.strIsNull(username)) {
			username = Util.getTrimParameter(request, "username");
		}
		if (Util.strIsNull(password)) {
			password = Util.getTrimParameter(request, "password");
		}
		String host = Util.getTrimParameter(request, "host");
		String _alarmPort = Util.getTrimParameter(request, "alarmPort");

		if (host.length() == 0) {// get the client host
			host = request.getRemoteAddr();
			if (host == null || host.length() == 0) {
				host = request.getRemoteHost();
			}
		}

		VssValidator.validateMaxLength("field.user.username", username, true,
				32);
		VssValidator.validateMaxLength("field.user.password", password, false,
				32);
		VssValidator.validateNumber("field.alarmPort", _alarmPort, false,
				VssConstants.DATA_INTEGER);
		VssValidator.validateRequired("field.host", host);

		// set session data

		
				nasType = 2;// CSG
			
		if(log.isDebugEnabled()){
		log.debug("InterDomainAuthenticate by radius server ... ");
		}
	   ServerAddress serverAddress=RadiusAuthenticate.getServerAddress(request);
		
		CrossDomainCredential cdc=new CrossDomainCredential();
		
		RadiusAuthenticate.authenticate(username, password, serverAddress.getServerIp(), serverAddress.getServerPort(), serverAddress.getServerId(),null,localIp,
				nasType,vssRadiusClient,cdc,false,serverAddress.getFront_type());
//		added vondart
		
		log.debug("InterDomainAuthByRadius succeed!");
		
		// get user info form database
		Map parameters = new LinkedHashMap();
		
		parameters.put("username", username);
		parameters.put("nextDomainUsername", cdc.getNextDomainUserName());
		parameters.put("nextDomainPassword", cdc.getNextDomainPassWord());//Util.MD5(
		// set parameters xml string, then return
		response.setParameters(parameters);
		return response;

	}
	
	public void setLocalIp(String localIp) {
		if(!Util.strIsNull(localIp)){
			localIp = localIp.trim(); 
		}
		this.localIp = localIp;
	}

	public void setNasType(int nasType) {
		this.nasType = nasType;
	}

	public void setVssRadiusClient(VssRadiusClient client) {
		this.vssRadiusClient = client;
	}

}
